using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using OpenComposite.EII.Designer.Generic;
using OpenComposite.Base.Designer;
using System.Xml;
using System.Xml.Schema;

namespace OpenComposite.EII.Designer.BRules
{
	public class ONERRORResultSymbol : ActionSymbol
	{
		#region Initialization
		public ONERRORResultSymbol()
			: base()
		{
			init();
		}
		private void init()
		{
			this.BeginUpdate();

			this.Text = "ON ERROR";
			this.ResultString = "";

			this.Anchor = System.Windows.Forms.AnchorStyles.Right | System.Windows.Forms.AnchorStyles.Top;

			this.Border = BorderType.Rectangle;
			this.BorderColor = Color.Red;
			this.BorderWidth = 3;

			Rectangle rc = this.CalculateClientArea();
			txtResult.Size = new Size(rc.Width, rc.Height - txtResult.Top);
			txtResult.TextChanged += new EventHandler(txtResult_TextChanged);
			txtResult.IsEditingChanged += new EventHandler(txtResult_IsEditingChanged);

			this.ContextMenuStrip = null;

			this.EndUpdate();
		}

		void txtResult_IsEditingChanged(object sender, EventArgs e)
		{
			if ( !txtResult.IsEditing ) {
				autoSize();
			}
		}

		void txtResult_TextChanged(object sender, EventArgs e)
		{
			//autoSize();
		}

		private void autoSize()
		{
			BusinessRuleSymbol symbol = this.Parent as BusinessRuleSymbol;
			if ( symbol == null ) return;
			symbol.ResizeThenElseErrors();
			//Graphics g = Graphics.FromHwnd(this.DesignerViewControl.Handle);
			//if ( g == null ) return;
			//SizeF size = g.MeasureString(txtResult.Text, txtResult.Font, new SizeF(0, txtResult.Height),
			//    new StringFormat(
			//        StringFormatFlags.DirectionRightToLeft |
			//        StringFormatFlags.FitBlackBox |
			//        StringFormatFlags.LineLimit));
			//Size old = this.Size;
			//old = old - txtResult.Size;
			//old = old + Size.Ceiling(size);
			//// always default height!
			//old.Height = 50;
			//if ( old.Width < 100 ) old.Width = 100;
			//if ( this.Parent != null ) {
			//    int newParentWidth = ( this.Parent.Width - this.Width + old.Width );
			//    if ( newParentWidth > this.Parent.Width ) {
			//        this.Parent.Size = new Size(newParentWidth, this.Parent.Size.Height);
			//    }
			//}
			//foreach ( DesignerControl ctrl in this.Parent.Controls ) {
			//    if ( ctrl is ELSEResultSymbol || ctrl is THENResultSymbol || ctrl is ONERRORResultSymbol ) {
			//        if ( ctrl.Width < this.Width ) {
			//            ctrl.Size = old;
			//        } else {
			//            this.Width = ctrl.Width;
			//        }
			//    }
			//}
		}
		#endregion

		#region Public Members

		#region Properties
		public override bool CanSelect
		{
			get
			{
				return true;
			}
		}
		public override bool CanResize
		{
			get
			{
				return true;
			}
		}
		public override OpenComposite.Base.Designer.ResizeSides ResizableSides
		{
			get
			{
				return OpenComposite.Base.Designer.ResizeSides.Left;
			}
		}
		#endregion

		#region Methods

		#region Drag&Drop
		public override void DropObject(object obj, Point location, System.Windows.Forms.MouseButtons button)
		{
			//if ( this.IsDropAllowed(obj, location, button) && button == System.Windows.Forms.MouseButtons.Right ) {
			//    this.DesignerViewControl.AddRoute((DesignerControl)obj, this);
			//}
		}
		public override bool IsDropAllowed(object obj, Point location, System.Windows.Forms.MouseButtons button)
		{
			return false;
		}
		#endregion

		#endregion

		#endregion

		#region Protected Members

		#region Methods
		protected override Rectangle CalculateClientArea()
		{
			Rectangle rc = this.Bounds;
			rc.Inflate(-5, -5);
			return rc;
		}
		#endregion

		#endregion

		#region IXmlSerializable Members

		public override XmlSchema GetSchema()
		{
			return null;
		}

		public override void ReadXml(XmlReader reader)
		{
			while ( reader.Read() ) {
				switch ( reader.Name ) {
					case "DesignerControl":
						if ( reader.NodeType == XmlNodeType.EndElement )
							return;
						break;
					case "RESULTSTRING":
						this.ResultString = reader.ReadString();
						break;
					default:
						ReadStandardProperties(reader);
						break;
				}
			}
		}

		public override void WriteXml(XmlWriter writer)
		{
			WriteStandardProperties(writer);

			// Start RESULTSTRING
			writer.WriteStartElement("RESULTSTRING");
			writer.WriteString(this.ResultString);
			writer.WriteEndElement();
			// End RESULTSTRING
		}

		#endregion
	}
}
